package com.zsh.rabbitmq.five;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.zsh.rabbitmq.utils.RabbitMQUtils;

/**
 * @author zsh
 * @Description 发布订阅模式，接收者02
 * @create 2022-05-05 17:06
 */
public class ReceiveLogs02 {
    //交换机名称
    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMQUtils.getChannel();

        //声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        //声明队列  临时队列
        /*
            生成一个临时队列，队列名称是随机的
            当消费者断开队列链接时，队列自动删除
         */
        String queue = channel.queueDeclare().getQueue();

        //绑定交换机与队列
        channel.queueBind(queue, EXCHANGE_NAME, "");
        System.out.println("ReceiveLogs02 等待接收消息......");

        //接收消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            String msg = new String(message.getBody(), "UTF-8");
            System.out.println("ReceiveLogs02 接收到的消息是：" + msg);
        };

        //取消消息

        channel.basicConsume(queue, true, deliverCallback, (CancelCallback) null);


    }
}
